import React from 'react'
import * as echarts from 'echarts'
import { useMount } from 'ahooks';
let myChart = ''
export default function Echarts() {
  const [options, setOptions] = React.useState({
    xAxis: {
      type: 'category',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
      type: 'value'
    },
    series: [{
      data: [150, 230, 224, 218, 135, 147, 260],
      type: 'line'
    }]
  })
  useMount(() => {
    myChart = echarts.init(document.getElementById('main'))
    myChart.setOption(options)
  })
  function draw (){

    myChart.setOption(options)
  }
  function changeData1 () {
    setOptions({
      xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
      },
      yAxis: {
        type: 'value'
      },
      series: [{
        data: [150, 230, 224, 218, 135, 147, 260],
        type: 'bar'
      }]
    })
    myChart.setOption(options)
  }
  function changeData2 () {
    setOptions({
      title: {
          text: '某站点用户访问来源',
          subtext: '纯属虚构',
          left: 'center'
      },
      tooltip: {
          trigger: 'item'
      },
      legend: {
          orient: 'vertical',
          left: 'left',
      },
      series: [
          {
              name: '访问来源',
              type: 'pie',
              radius: '50%',
              data: [
                  {value: 1048, name: '搜索引擎'},
                  {value: 735, name: '直接访问'},
                  {value: 580, name: '邮件营销'},
                  {value: 484, name: '联盟广告'},
                  {value: 300, name: '视频广告'}
              ],
              emphasis: {
                  itemStyle: {
                      shadowBlur: 10,
                      shadowOffsetX: 0,
                      shadowColor: 'rgba(0, 0, 0, 0.5)'
                  }
              }
          }
      ]
  })
  myChart.setOption(options)
  }
  return (
    <div>
      <button onClick = { changeData1 }>改变数据-柱状图</button>
      <button onClick = { changeData2 }>改变数据-饼状图</button>
      <div id="main" style={{ width: '600px',height: '500px'}}></div>
    </div>
  );
}
